package month202011;

public class MyCircularQueue {

    class DoudleCircularLinkedNode {
        private int value;
        private DoudleCircularLinkedNode prev;
        private DoudleCircularLinkedNode next;

        public DoudleCircularLinkedNode() {
        }

        public DoudleCircularLinkedNode(int value) {
            this.value = value;
        }
    }

    /**
     * 成员变量
     * 1、capacity 容量大小
     * 2、size 当前大小
     * 3、伪头节点
     */
    private int capacity;
    private int size;
    private DoudleCircularLinkedNode head;

    /**
     * Initialize your data structure here. Set the size of the queue to be k.
     */
    public MyCircularQueue(int k) {
        this.capacity = k;
        this.size = 0;
        this.head = new DoudleCircularLinkedNode();
        head.prev = head;
        head.next = head;

    }

    /**
     * Insert an element into the circular queue. Return true if the operation is successful.
     */
    public boolean enQueue(int value) {
        if (size >= capacity) {
            return false;
        }
        DoudleCircularLinkedNode newNode = new DoudleCircularLinkedNode(value);
        newNode.prev = head.prev;
        newNode.next = head;
        head.prev.next = newNode;
        head.prev = newNode;
        size++;
        return true;

    }

    /**
     * Delete an element from the circular queue. Return true if the operation is successful.
     */
    public boolean deQueue() {
        if (size <= 0) {
            return false;
        }
        head.next.next.prev = head;
        head.next = head.next.next;
        size--;
        return true;
    }

    /**
     * Get the front item from the queue.
     */
    public int Front() {
        if (size <= 0) {
            return -1;
        }
        return head.next.value;
    }

    /**
     * Get the last item from the queue.
     */
    public int Rear() {
        if (size <= 0) {
            return -1;
        }
        return head.prev.value;
    }

    /**
     * Checks whether the circular queue is empty or not.
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * Checks whether the circular queue is full or not.
     */
    public boolean isFull() {
        return size == capacity;

    }
}
